# _*_ coding:utf-8 _*_
import sys

import os
import time as t
import operator as op
sys.path.append('D:/WORKSPACE/python_workspace/myutils')
from myutils.ClientUtils import ClientUtils as client
from myutils.MyConfig import ConfigurationUtils as config


# 获得项目路径
path = os.path.dirname(sys.path[0])

# 实例化Hessian实例


# 获取请求文件,请求文件名中包含"request"字符将被调用,否则忽略
def find_method_files():
    request_list = []
    if len(sys.argv) == 2:
        parent_path = path + '/files/' + sys.argv[1] + '/'
        request_list = map(lambda y: parent_path + y, os.listdir(path + '/files/' + sys.argv[1]))
    else:
        for versions in os.listdir(path + '/files/'):
            parent_path = path + '/files/' + versions + '/'
            request_list += map(lambda y: parent_path + y, os.listdir(parent_path))
    return request_list


def list_request_file(dir):
    if os.path.isdir(dir):
        return map(lambda y: dir + '/' + y, filter(lambda x: x.find('request') != -1,
                                                          os.listdir(dir)))


# 执行调用
def invoke_call(request_list, remote_client):
    for request_path in request_list:
        with open(request_path, 'r+', encoding='UTF-8') as requestFile:
            content = requestFile.read()

            if len(content) > 0:
                contents = map(lambda x: x.strip(), content.split('@@@@@'))
                request = contents.__next__()
                expect = contents.__next__().replace('\r', '')
                response = remote_client.call(request).strip().replace('\r', '')
                if op.eq(response, expect):
                    print('-------success-------' + request_path)
                else:
                    print('-------failed-------' + request_path)
                requestFile.truncate()
                requestFile.seek(0)
                t.sleep(0.1)
                requestFile.write(request + '\r @@@@@  \r' + expect + '\r @@@@@ \r' + response)


requestList = find_method_files()

for dir in requestList:
    if os.path.isdir(dir):
        conf = config(dir + '/config.ini')
        if conf.get_value('ignore') != 'true':
            remote_client = client(conf)
            invoke_call(list_request_file(dir),remote_client)
